package org.syyo.admin.utils;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Auther: wangzhong
 * @Date: 2019/10/16 09:43
 * @Description:
 */
public class MyStringUtils {

    /**
     * 将字符串转成列表  "0,2,4" => [0,2,4]
     * @param str
     * @return
     */
    public static List<Integer> stringToList(String str){
        List<Integer> list = new ArrayList<>();
        if (!StringUtils.isEmpty(str)){
            String[] splits = str.split(",");
            for (String split : splits) {
                list.add(Integer.parseInt(split));
            }
        }
        return list;
    }


    /**
     * 将整型列表转换为字符串 [0,2,4] => "0,2,4"
     * @param list
     * @return
     */
    public static String listToString(List<Integer> list){
        String pids =  "";
        if (list != null && list.size()>0){
            //逆序
            Collections.reverse(list);
            int size = list.size();
            for (int i =0 ;i < size;i++) {
                if (i == 0) {
                    pids =  list.get(i) + "" ;
                } else {
                    pids = list.get(i) + "," + pids;
                }
            }
        }else {
            pids =  "0";
        }
        return pids;
    }

    public static boolean isNotEmpty(String str) {
        return str != null && !"".equals(str);
    }

    public static boolean isEmpty(String str) {
        return str == null || "".equals(str);
    }

}
